/**
 * @filename:SwitchOrderInfoController 2020年06月11日
 * @project hotel-switch  V1.0
 * Copyright(c) 2020 Zhang·JZ Co. Ltd.
 * All right reserved.
 */
package hotel.switchs.oversea.biz.controller.order;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import hotel.base.oversea.constant.CommonConstants;
import hotel.base.oversea.constant.enums.CommonEnums;
import hotel.base.oversea.utils.Result;
import hotel.switchs.oversea.api.VO.order.OrderListSearchVO;
import hotel.switchs.oversea.api.entity.order.SwitchOrderPool;
import hotel.switchs.oversea.biz.service.SwitchOrderOperateService;
import hotel.switchs.oversea.biz.service.SwitchOrderPoolService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Description: 携程订单控制类
 * @Author: Zhang·jz
 * @date: 2020/6/8
 */
@Slf4j
@RestController
@RequestMapping("/switchOrderPool")
@Api(value = "switchOrderPoolController", tags = {"获取携程订单信息接口"})
public class SwitchOrderPoolController {

    @Autowired
    SwitchOrderPoolService switchOrderPoolService;

    @Autowired
    private SwitchOrderOperateService switchOrderOperateService;

    /**
     * 获取系统状态为拒绝的延迟单态
     *
     * @return
     */
    @PostMapping("/getDelayRejectBookingOrders")
    @ApiOperation(value = "获取系统状态为拒绝的延迟单", notes = "必须经过auth授权")
    @ApiImplicitParam(name = "getRejectBookingOrders")
    public Result<Object> getRejectChangeBookingOrders(){
        return switchOrderPoolService.getDelayRejectBookingOrders();//延迟单且状态为拒绝
    }

    /**
     * 获取需要改为立即下单的延迟订单ids
     *
     * @return
     */
    @GetMapping("/getDelayChangeBookingOrders")
    @ApiOperation(value = "获取需要改为立即下单的延迟订单ids", notes = "必须经过auth授权")
    @ApiImplicitParam(name = "getDelayChangeBookingOrders")
    public Result<Object> getDelayChangeBookingOrders(){
        return switchOrderPoolService.getDelayChangeBookingOrders();
    }

    /**
     * 更改pool表 订单状态
     * mtOrderState  ：  美团订单状态
     *
     * @return
     */
    @PostMapping("/updatePoolIsOrderByIds")
    @ApiOperation(value = "批量修改订单的立即下单状态", notes = "必须经过auth授权")
    @ApiImplicitParam(name = "updatePoolIsOrderByIds", value = "jsonObject", required = true, dataType = "JSONObject")
    public Result<Object> updatePoolIsOrderByIds(@RequestBody JSONObject jsonObject){
        List<String> mdKeyList  = (List<String>)jsonObject.get("ids");//ids指的是md5Key
        if (ObjectUtils.isEmpty(mdKeyList)) {
            return new Result<>(CommonEnums.FIELD_NULL,"mdKeyList 不可为空");
        }
        return switchOrderPoolService.updatePoolIsOrderByMdkey(mdKeyList);
    }

    /**
     * 应用场景：美团异步通知后修改pool表对应状态
     *      * @param jsonObject
     * @return
     */
    @PostMapping("/updatePoolMtStateByOrderId")
    public Result updatePoolMtStateByOrderId(@RequestBody Map map) {
        String orderId = (String) map.get("orderId");
        String mtOrderState = (String) map.get("mtOrderState");
        Integer sysOrderState =(Integer) map.get("sysOrderState");
        Integer isOrder = (Integer) map.get("isOrder");
        Integer orderWaitTime = (Integer) map.get("orderWaitTime");
        String isOrderRemark = (String) map.get("isOrderRemark");
        String logRemark = (String) map.get("logRemark");
        log.info("updatePoolMtStateByOrderId={}", JSON.toJSONString(map));
        return switchOrderOperateService.updatePoolMtStateByOrderId(orderId,mtOrderState,sysOrderState,isOrder,orderWaitTime,isOrderRemark,logRemark);
    }

    public static void main(String[] args) {
        JSONObject jsonObject = new JSONObject();
        System.out.printf(jsonObject.getInteger("sysOrderState")+"");
    }

    /**
     * 用途：处理下单订单器调用 SwitchHotelOrderFromMeituanJob
     *
     * @author Qiuyy
     * @date 2020/08/17
     */
    @PostMapping("/getSwitchOrderInfoByDltAsc")
    public Result aspgetSwitchOrderInfoByDltAsc() {
        return switchOrderOperateService.getSwitchOrderInfoByDltAsc();
    }

    /**
     * <H1>更新订单状态</H1>
     *
     * @param
     * @return hotel.base.oversea.utils.Result
     * @author HuangT
     * @date 2020/7/9 16:14
     */
    @PostMapping("/updateOrderResult")
    public Result updateOrderResult(@RequestBody SwitchOrderPool switchOrderPool) {
        log.info("updateOrderResult switchOrderPool={}", JSON.toJSONString(switchOrderPool));
        switchOrderPoolService.updateOrderResult(switchOrderPool);
        return new Result<>("更新成功!",CommonConstants.SUCCESS);
    }

    /**
     * <H1>获取订单列表</H1>
     *
     * @param
     * @param
     * @return hotel.base.oversea.utils.Result<java.lang.Object>
     * @author HuangT
     * @date 2020/7/21 16:17-
    */
    @PostMapping("/getOrderList")
    public Result<Object> getOrderList(@RequestBody(required = false) OrderListSearchVO orderListSearchVO) {
        Result<Object> ret = switchOrderPoolService.getOrderList(orderListSearchVO);
        return ret;
    }


    /**
     * <H1>人工采购</H1>
     *
     * @param orderId 实体对象
     * @return hotel.base.oversea.utils.Result<java.lang.Object>
     * @author HuangT
     * @Date 2020/9/7 15:00
     */
    @GetMapping("/updateOrderState")
    public Result<Object> updateOrderState(String orderId) {
        if(ObjectUtils.isEmpty(orderId)){
            return  new Result<>("orderId 不可为空", CommonConstants.REQUEST_ERROR);
        }
        SwitchOrderPool switchOrderPool = new SwitchOrderPool();
        switchOrderPool.setOrderId(orderId);
        return switchOrderPoolService.updateOrderStateAboutRgxd(switchOrderPool);
    }

    /**
     * <H1>人工：拆单：亏损1直采</H1> 拆单后亏损40，拆弹前亏损30，用第一次亏损的方式直接采购
     *
     * @param orderId 实体对象
     * @return hotel.base.oversea.utils.Result<java.lang.Object>
     * @author HuangT
     * @Date 2020/9/7 15:00
     */
    @GetMapping("/updateOrderStateAboutLossBefore")
    public Result<Object> updateOrderStateAboutLossBefore(String orderId) {
        if(ObjectUtils.isEmpty(orderId)){
            return  new Result<>("orderId 不可为空", CommonConstants.REQUEST_ERROR);
        }
        SwitchOrderPool switchOrderPool = new SwitchOrderPool();
        switchOrderPool.setOrderId(orderId);
        return switchOrderPoolService.updateOrderStateAboutLossBefore(switchOrderPool);
    }
    /**
     * <H1>人工：拆单：亏损2直采</H1> 拆单前亏损50，拆单后亏损20，用拆单后亏损的方式直接采购
     *
     * @param orderId 实体对象
     * @return hotel.base.oversea.utils.Result<java.lang.Object>
     * @author HuangT
     * @Date 2020/9/7 15:00
     */
    @GetMapping("/updateOrderStateAboutLossLast")
    public Result<Object> updateOrderStateAboutLossLast(String orderId) {
        if(ObjectUtils.isEmpty(orderId)){
            return  new Result<>("orderId 不可为空", CommonConstants.REQUEST_ERROR);
        }
        SwitchOrderPool switchOrderPool = new SwitchOrderPool();
        switchOrderPool.setOrderId(orderId);
        return switchOrderPoolService.updateOrderStateAboutLossLast(switchOrderPool);
    }


    /**
     * <H1>人工拒单</H1>
     *
     * @param orderId 实体对象
     * @return hotel.base.oversea.utils.Result<java.lang.Object>
     * @author HuangT
     * @Date 2020/9/7 15:00
     */
    @GetMapping("/updateOrderStateAboutReject")
    public Result<Object> updateOrderStateAboutReject(String orderId) {
        if(ObjectUtils.isEmpty(orderId)){
            return  new Result<>("orderId 不可为空", CommonConstants.REQUEST_ERROR);
        }
        return switchOrderPoolService.updateOrderStateAboutReject(orderId);
    }

    /**
     * <H1>锁单  将延迟单改为人工单定时器不再处理</H1>
     *
     * @param orderId 实体对象
     * @return hotel.base.oversea.utils.Result<java.lang.Object>
     * @author HuangT
     * @Date 2020/9/7 15:00
     */
    @GetMapping("/rgPoolOrder")
    public Result<Object> rgPoolOrder(String orderId) {
        if(ObjectUtils.isEmpty(orderId)){
            return  new Result<>("orderId 不可为空", CommonConstants.REQUEST_ERROR);
        }
        return switchOrderPoolService.rgPoolOrder(orderId);
    }

    /**
     * <H1>解锁订单   将人工处理订单改为延时单</H1>
     *
     * @param orderId 实体对象
     * @return hotel.base.oversea.utils.Result<java.lang.Object>
     * @author HuangT
     * @Date 2020/9/7 15:00
     */
    @GetMapping("/unlockPoolOrder")
    public Result<Object> unlockPoolOrder(String orderId) {
        if(ObjectUtils.isEmpty(orderId)){
            return  new Result<>("orderId 不可为空", CommonConstants.REQUEST_ERROR);
        }
        return switchOrderPoolService.unlockPoolOrder(orderId);
    }


    /**
    *@Description: 人工采购修改美团订单号
    *@Param: map
    *@return: Result<Object>
    *@Author: Zhang·jz
    *@date: 2020/12/16
    */
    @PostMapping("/updateMtOrderIdByOrderId")
    public Result<Object> updateMtOrderIdByOrderId(@RequestBody Map map) {
        if(ObjectUtils.isEmpty(map)){
            return  new Result<>(CommonEnums.DATA_NULL);
        }

        log.info("order:{} ; meituanOrderId:{} ; mtDistributorOrderId:{}",map.get("orderId"),map.get("meituanOrderId"),map.get("mtDistributorOrderId"));

        return switchOrderPoolService.updateMtOrderIdByOrderId(map);
    }
}
